/*
Copyright (C) <2011>  <ivan vaccari>

    <ivan.vaccari@studenti.unipr.it>


This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.


*/
#include "instruction.h"
#include <cstdio>
instruction::instruction(const std::string &line):error("no errors in this instruction"){
    char qi[20];
    char qf[20];
    char s1[20];
    char s2[20];
    char direction;
    if (sscanf(line.c_str(),"%s %s %s %s %c",qi,s1,qf,s2,&direction)<5){
        throw "invalid instruction: "+line;
        valid=false;
    }else{
        valid=true;
        inState=std::string(qi);
        outState=std::string(qf);
        inSymbol=std::string(s1);
        outSymbol=std::string(s2);

        if (direction=='R' || direction=='r')
            move=0;
        else if (direction=='L' || direction=='l')
            move=1;
        else{
            valid=false;
            throw "invalid instruction: "+line;
        }

    }



}
instruction::instruction():error("no errors in this instruction"),valid(false){
}
bool instruction::isValid() const{
    return valid;
}
std::string instruction::getNextState() const{
    return outState;
}
std::string instruction::getSymbolToWrite() const{
    return outSymbol;
}
bool instruction::moveLeft(){
    return (move==1);
}
bool instruction::moveRight(){
    return (move==2);
}
bool instruction::match(const std::string &state,const std::string &symbol) const {
    return (state==inState)&&(symbol==inSymbol);
}
